﻿using System;
using System.Windows.Forms;

namespace PlaneBombGame
{
    public partial class LoginRegisterForm : Form
    {
        public LoginRegisterForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text;

            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("用户名和密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (username.Contains(" ") || password.Contains(" "))
            {
                MessageBox.Show("用户名和密码中不能包含空格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (UserManager.Login(username, password))
            {
                MessageBox.Show("登录成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text;

            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("用户名和密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (username.Contains(" ") || password.Contains(" "))
            {
                MessageBox.Show("用户名和密码中不能包含空格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (UserManager.Register(username, password))
            {
                MessageBox.Show("注册成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("用户名已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            if (txtUsername.Text.Contains(" "))
            {
                MessageBox.Show("用户名中不能包含空格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUsername.Text = txtUsername.Text.Trim();
                txtUsername.SelectionStart = txtUsername.Text.Length;
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            if (txtPassword.Text.Contains(" "))
            {
                MessageBox.Show("密码中不能包含空格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Text = txtPassword.Text.Trim();
                txtPassword.SelectionStart = txtPassword.Text.Length;
            }
        }

        private void LoginRegisterForm_Load(object sender, EventArgs e)
        {
            txtUsername.Focus();
        }
    }
}